﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace JustNotJewelryMain
{
    public class Renderer
    {
        private int visibleRows { get; set; }
        private int visibleCols { get; set; }
        public char[,] GridMatrix { get; set; }
        
        public Renderer(int rows, int cols)
        {
            this.visibleRows = rows;
            this.visibleCols = cols;
            this.GridMatrix = new char[rows, cols];
        }

        public void EnqueueItem(GameObject obj)
        {
            char[,] image = obj.GetImage();
            Coordinates topLeft = obj.GetTopLeftCorner();

            int sizeInRows = image.GetLength(0); //End row 
            int sizeInCols = image.GetLength(1); //End col

            int startRow = topLeft.Row;
            int startCol = topLeft.Col;

            for (int row = startRow; row < startRow + sizeInRows; row++)
            {
                for (int col = startCol; col < startCol + sizeInCols; col++)
                {
                    GridMatrix[row, col] = image[(row - startRow), (col - startCol)];
                }
            }
        }

        public void RenderAll(List<GameObject> objects, List<GameObject> tableInfo)
        {
            Console.SetCursorPosition(0, 0);

            RenderInfoTable(tableInfo);
            
            for (int row = 6; row < this.visibleRows; row++)
            {
                for (int col = 0; col < this.visibleCols; col++)
                {
                    ConsoleColor currentColor = ConsoleColor.White;
                    if (this.GridMatrix[row, col] != ' ')
                    {
                        var getObject = objects.FirstOrDefault(element => element.TopLeft.Row == row && element.TopLeft.Col == col );

                        for (int i = 0; i < objects.Count; i++)
                        {
                            if (objects[i] is FallingBlock)
                            {
                                currentColor = objects[i].ObjectColor;
                                break;
                            }
                        }

                        if (getObject != null)
                        {   
                            currentColor = getObject.ObjectColor;       
                        }
                    }
                    Console.ForegroundColor = currentColor;
                    Console.Write(this.GridMatrix[row, col]);
                }
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write('|');
                Console.WriteLine();
            }
            Console.WriteLine(new string('-', this.GridMatrix.GetLength(1)));
        }

        //Method for printing only the informational table
        public void RenderInfoTable(List<GameObject> obj)
        {   
            for (int row = 0; row < 6; row++)
            {
                for (int col = 0; col < visibleCols; col++)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write(this.GridMatrix[row, col]);
                }
                Console.WriteLine();
            }
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(new string('-', this.GridMatrix.GetLength(1)));
            
        }

        //Fills the grid with empty characters to simulate space and clear the grid
        public void ClearFieldGrid()
        {   
            for (int row = 0; row < this.visibleRows; row++)
            {
                for (int col = 0; col < this.visibleCols; col++)
                {
                    this.GridMatrix[row, col] = ' ';
                }
            }
        }
    }
}
